<?php
App::uses('AppController', 'Controller');
/**
 * Prototypes Controller
 *
 * @property Prototype $Prototype
 */
class PrototypesController extends AppController {


	public function beforeFilter() {
		parent::beforeFilter();
	}

/**
 * index method
 *
 * @return void
 */
	public function admin_index() {
		$this->Prototype->recursive = 0;
		$this->set('prototypes', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		$this->Prototype->id = $id;
		if (!$this->Prototype->exists()) {
			throw new NotFoundException(__('Invalid prototype'));
		}
		$prototype = $this->Prototype->read(null, $id);
		#$this->set('prototype', $this->Prototype->findBySlug($slug));
	}

/**
 * add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->Prototype->create();
			if ($this->Prototype->saveAll($this->request->data)) {
				$this->Session->setFlash(__('The prototype has been saved'), 'default', array(), 'good');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The prototype could not be saved. Please, try again.'), 'default', array(), 'bad');
			}
		}
		$properties = $this->Prototype->Property->find('list');
		$optionTypes = $this->Prototype->OptionType->find('list', array('conditions' => array('status' => OptionType::show)));
		$this->set(compact('properties'));
		$this->set(compact('optionTypes'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		$this->Prototype->id = $id;
		if (!$this->Prototype->exists()) {
			throw new NotFoundException(__('Invalid prototype'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Prototype->save($this->request->data)) {
				$this->Session->setFlash(__('The prototype has been saved'), 'default', array(), 'good');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The prototype could not be saved. Please, try again.'), 'default', array(), 'bad');
			}
		} else {
			$this->request->data = $this->Prototype->read(null, $id);
			$properties = $this->Prototype->Property->find('list');
			$optionTypes = $this->Prototype->OptionType->find('list', array('conditions' => array('status' => OptionType::show)));
			$this->set(compact('properties'));
			$this->set(compact('optionTypes'));
		}
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Prototype->id = $id;
		if (!$this->Prototype->exists()) {
			throw new NotFoundException(__('Invalid prototype'));
		}
		if ($this->Prototype->delete()) {
			$this->Session->setFlash(__('Prototype deleted'), 'default', array(), 'good');
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Prototype was not deleted'), 'default', array(), 'bad');
		$this->redirect(array('action' => 'index'));
	}
}
